package com.scoy.honeymei.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.MainActivity;
import com.scoy.honeymei.activity.WebActivity;
import com.scoy.honeymei.activity.home.FirstDialog;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.LoginBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.LogUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: LoginActivity$
 * @Description: 登录界面
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 10:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 10:20$
 * @UpdateRemark: 更新内容
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.lg_sign_tv)
    TextView lgSignTv;
    @BindView(R.id.lg_phone_et)
    EditText lgPhoneEt;
    @BindView(R.id.lg_yzm_et)
    EditText lgYzmEt;
    @BindView(R.id.lg_yzm_tv)
    TextView lgYzmTv;
    @BindView(R.id.lg_yzm_llt)
    LinearLayout lgYzmLlt;
    @BindView(R.id.lg_login_tv)
    TextView lgLoginTv;
    @BindView(R.id.lg_towechat_iv)
    ImageView lgTowechatIv;
    @BindView(R.id.lg_xieyi_tv)
    TextView lgXieyiTv;
    @BindView(R.id.cb)
    CheckBox cb;
    private LoginActivity mContext;
    private CountDownTimer countDownTimer;
    private String loginPhone = "Null";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mContext = LoginActivity.this;
        initNormal();

        if (SPHelper.getInt("sure", 0) != 1) {
            FirstDialog firstDialog = FirstDialog.newInstance();
            firstDialog.setOutImp(() -> {
                mContext.finish();
            });
            firstDialog.show(getSupportFragmentManager(), "xieyi");
        }

    }

    @Override
    public void initNormal() {
        MyUtil.setStatusBar(mContext, getWindow(), false, 0);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
    }

    @OnClick({R.id.lg_yzm_tv, R.id.lg_login_tv, R.id.lg_towechat_iv, R.id.lg_xieyi_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lg_yzm_tv:
                if (TextUtils.isEmpty(lgPhoneEt.getText())) {
                    return;
                }
                if (!CheckUtil.isMobileNO(lgPhoneEt.getText().toString())) {
                    MyUtil.mytoast(mContext, "请输入正确的手机号");
                    return;
                }
                httpgetCode();
                break;
            case R.id.lg_login_tv:
                if (MyUtil.isFastClick()) {
                    preLogin();
                }
                break;
            case R.id.lg_towechat_iv:
                //删除授权  UMShareAPI.get(mContext).deleteOauth(mContext, SHARE_MEDIA.WEIXIN, authListener);
                if (UMShareAPI.get(mContext).isInstall(mContext, SHARE_MEDIA.WEIXIN)) {
                    UMShareAPI.get(mContext).getPlatformInfo(mContext, SHARE_MEDIA.WEIXIN, authListener);
                } else {
                    MyUtil.mytoast(mContext, "未检测到微信客户端");
                }

                break;

            case R.id.lg_xieyi_tv:
                Intent intent2 = new Intent(mContext, WebActivity.class);
                intent2.putExtra("webType", 2);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        UMShareAPI.get(this).release();
    }

    //验证码
    private void httpgetCode() {
        HttpParams params = new HttpParams();
        params.put("phone", lgPhoneEt.getText().toString());
        //1=登录、2=绑定手机号
        params.put("type", 1);
        HpGo.newInstance()
                .HttpGo(mContext, MyUrl.GET_CODE, params, new HpCallback() {
                    @Override
                    public void onSuccess(String o) {
                        loginPhone = lgPhoneEt.getText().toString();
                        countDownTimer = MyUtil.startTime(lgYzmTv);
                        MyUtil.mytoast(mContext, (String) o);
                    }

                    @Override
                    public void onFailed(String code, String msg, String o) {
                        MyUtil.mytoast(mContext, code + "," + msg);
                    }

                    @Override
                    public void onError(int code, String errorMsg) {
                        MyUtil.mytoast(mContext, errorMsg);
                    }
                });

    }

    private void preLogin() {
        if (TextUtils.isEmpty(lgPhoneEt.getText())) {
            MyUtil.mytoast(mContext, "请输入手机号");
            return;
        }
        if (!CheckUtil.isMobileNO(lgPhoneEt.getText().toString())) {
            MyUtil.mytoast(mContext, "请输入正确的手机号");
            return;
        }

        if (TextUtils.isEmpty(lgYzmEt.getText())) {
            MyUtil.mytoast(mContext, "请输入验证码");
            return;
        }
      /*  if (!loginPhone.equals(lgPhoneEt.getText().toString())) {
            MyUtil.mytoast(mContext, "0，手机号或验证码错误");
             return;
        }*/

        if(!cb.isChecked()){
            MyUtil.mytoast(mContext, "请查看并同意用户协议");
            return;
        }

        httpLogin();
    }

    //登录
    private void httpLogin() {
        HttpParams params = new HttpParams();
        params.put("phone", lgPhoneEt.getText().toString());
        params.put("code", lgYzmEt.getText().toString());
        HpGo.newInstance().HttpGo(mContext, MyUrl.TO_LOGIN, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                LoginBean loginBean = new Gson().fromJson((String) o, LoginBean.class);

                SPHelper.applyString(MeConstant.PHONE, loginBean.getPhone());
                SPHelper.applyString(MeConstant.UID, loginBean.getId() + "");

                Intent intent0 = new Intent(mContext, MainActivity.class);
                startActivity(intent0);
                finish();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    //微信登录
    private void httpWechatLogin(String uid) {
        HttpParams params = new HttpParams();
        params.put("openid", uid);
        HpGo.newInstance().HttpGo(mContext, MyUrl.CHECK_WECHAT_LOGIN, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                LoginBean loginBean = new Gson().fromJson(o, LoginBean.class);

                int is_login = loginBean.getIs_login();
                if (is_login == 1) {
                    SPHelper.applyString(MeConstant.PHONE, loginBean.getPhone());
                    SPHelper.applyString(MeConstant.UID, loginBean.getId() + "");
                    Intent intent0 = new Intent(mContext, MainActivity.class);
                    startActivity(intent0);
                    finish();
                } else {
                    Intent intent = new Intent(mContext, AttachActivity.class);
                    intent.putExtra("openid", uid);
                    startActivityForResult(intent, 0xaa);
                }

            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }


    UMAuthListener authListener = new UMAuthListener() {

        @Override
        public void onStart(SHARE_MEDIA platform) {
            LogUtil.e(platform.getName().toString());
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            LogUtil.e(data.toString());
            String uid = data.get("uid");
            String name = data.get("name");
            String iconurl = data.get("iconurl");
            String gender = data.get("gender");

            /*if (platform == SHARE_MEDIA.QQ) {// 1:微信 2：qq
                http3Login(uid, 2, data);
            } else if (platform == SHARE_MEDIA.WEIXIN) {
                http3Login(uid, 1, data);
            }*/

            httpWechatLogin(uid);

            //    Intent intent = new Intent(mContext, AttachActivity.class);
           /* intent.putExtra("type", type0);
            intent.putExtra("data", (Serializable) data);*/
            //  startActivityForResult(intent, 0xaa);

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            LogUtil.e("失败：" + t.getMessage());
            Toast.makeText(mContext, "失败：" + t.getMessage(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            LogUtil.e("取消了");
            Toast.makeText(mContext, "取消了", Toast.LENGTH_SHORT).show();
        }
    };


}
